import request from "../../common/utils/request";

const bill = {
  state: {
    billMatchTurnoverInit: {},
    billMatchAccountDetail: {},
  },
  mutations: {
    SET_BMT_INIT: (state, data) => {
      state.billMatchTurnoverInit = data;
    },
    SET_MA_DETAIL: (state, data) => {
      state.billMatchAccountDetail = {
        ...state.billMatchAccountDetail,
        ...data,
      };
    },
    CLEAR_MA_DETAIL: (state) => {
      state.billMatchAccountDetail = {};
    },
  },
  actions: {
    SetBMTInit: ({ commit, state }) => {
      return new Promise((resolve) => {
        if (Object.keys(state.billMatchTurnoverInit).length) {
          resolve(state.billMatchTurnoverInit);
        } else {
          request.get("/org", "/bill/streamListInit").then((res) => {
            if (res.initData) {
              commit("SET_BMT_INIT", res.initData);
              resolve(res.initData);
            }
          });
        }
      });
    },
    SetMADetail: ({ commit }, dataObj) => {
      commit("SET_MA_DETAIL", dataObj);
    },
    ClearMADetail: ({ commit }) => {
      commit("CLEAR_MA_DETAIL");
    },
  },
};

export default bill;
